package risk.control.application.utils;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class DateTimePickerUtil {

    public interface DateTimePickerListener {
        void onDateTimeSelected(String dateTimeString);
    }

    /**
     * 显示日期时间选择器
     *
     * @param context  上下文
     * @param listener 选择完成后的回调
     */
    public static void showDateTimePicker(Context context, final DateTimePickerListener listener) {
        // 获取当前日期
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        // 先显示日期选择器
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
                // 日期选择完成后，更新时间并显示时间选择器
                calendar.set(selectedYear, selectedMonth, selectedDay);
                showTimePicker(context, calendar, listener);
            }
        }, year, month, day);

        datePickerDialog.show();
    }

    /**
     * 显示时间选择器
     *
     * @param context  上下文
     * @param calendar 已选择的日期
     * @param listener 选择完成后的回调
     */
    private static void showTimePicker(Context context, final Calendar calendar, final DateTimePickerListener listener) {
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        TimePickerDialog timePickerDialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                // 时间选择完成后，更新calendar并格式化输出
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
                String dateTimeString = sdf.format(calendar.getTime());

                if (listener != null) {
                    listener.onDateTimeSelected(dateTimeString);
                }
            }
        }, hour, minute, true); // true表示24小时制

        timePickerDialog.show();
    }

    /**
     * 只显示日期选择器
     *
     * @param context  上下文
     * @param listener 选择完成后的回调
     */
    public static void showDatePicker(Context context, final DateTimePickerListener listener) {
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
                calendar.set(selectedYear, selectedMonth, selectedDay);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
                String dateString = sdf.format(calendar.getTime());

                if (listener != null) {
                    listener.onDateTimeSelected(dateString);
                }
            }
        }, year, month, day);

        datePickerDialog.show();
    }

    /**
     * 只显示时间选择器
     *
     * @param context  上下文
     * @param listener 选择完成后的回调
     */
    public static void showTimePicker(Context context, final DateTimePickerListener listener) {
        final Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        TimePickerDialog timePickerDialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
                String timeString = sdf.format(calendar.getTime());

                if (listener != null) {
                    listener.onDateTimeSelected(timeString);
                }
            }
        }, hour, minute, true);

        timePickerDialog.show();
    }
}
